/**
 * Created by guodong on 2017/11/23.
 */
'use strict';
angular.module('app')
    .controller('userCenterController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '慧驿站用户';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/userCenter/user/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/userCenter/user/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }


            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };


            $scope.search();

            $scope.clearSearch = function() {

                $scope.searchParams= { };
                $scope.search();
                $(".editSortBtn").show();
                $(".saveSortBtn").hide();
            }



            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };


            $scope.businessLicenses = [];
            $scope.preview = function(businessLicense){
                if (businessLicense != null && businessLicense != '') {
                    $scope.businessLicenses = businessLicense.split('|');
                }
                $scope.firstApply = true;
            }

            // 删除用户
            $scope.deleteById=function (id) {
                $scope.alertModel({
                    text:'确定删除？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/userCenter/user/deleteById',
                        data: {id:id}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.search();
                    });

                })

            }


            $scope.preview1 = function(userId,forbidStatus){
                $scope.userId =  userId;
                $scope.forbidStatus =  forbidStatus;
                $scope.firstApply = true;
            }
             // 取消禁言
            $scope.preview2 = function(userId,forbidStatus){
                $scope.alertModel({
                    text:'确定取消禁言？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name + '/userCenter/user/removeForbidUser',
                        data: {"userId":userId,"forbidStatus":forbidStatus}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.firstApply = false;
                        $scope.search();
                    });

                })

            }

            // Date.prototype.toLocaleString = function() {
            //     return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
            // };

           // 对用户进行禁言
            $scope.save = function(forbiddenTime,userId,forbidStatus){
                /*var today=new Date().getTime(),day;
                var forbiddenTime = $("#forbiddenTime").val();
                if(forbiddenTime=='1'){
                    day=today+24*60*60*1000;
                }else if(forbiddenTime=='2'){
                    day=today+7*24*60*60*1000;
                }else if(forbiddenTime=='3'){
                    day=today+30*24*60*60*1000;
                }else if(forbiddenTime=='4'){
                    day=today+6*30*24*60*60*1000;
                }else if(forbiddenTime=='5') {
                    day = today+100*30*24*60*60*1000;
                }
               /!* switch (forbiddenTime){
                    case '1':

                        break;
                    case '2':
                        today+=3600;
                        break;
                    case '3':
                        today+=7*24*60*60;
                        break;
                    case '4':
                        today+=30*24*60*60;
                        break;
                    case '5':
                        today+=6*30*24*60*60;
                        break;
                    case '6':
                        today=999999999;
                        break;
                }*!/
                var unixTimestamp = new Date(day);
                var forbiddenTime= unixTimestamp.toLocaleString();*/
                if(!$('#forbiddenTime').val()){
                    $scope.alertModal({'content':'禁言时间不能为空'});
                    return;
                }else{
                    $.ajax({
                        url : $scope.app.project_name + '/userCenter/user/ForbidUser',
                        data: {'forbiddenTime': $scope.forbiddenTime,"userId":userId,"forbidStatus":forbidStatus}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.firstApply = false;
                        $scope.search();
                    });
                }
            }

            // 查询标签表对应用户的标签
            $scope.addLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/userLabelList',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfos = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id=id;
                $scope.firstApply1 = true;
            }

            // 查询添加过的用户标签
            $scope.selectLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/userLabelAlready',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfoAdd = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id=id;
                $scope.firstApply2 = true;
            }

            $scope.oper = function (id,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/userCenter/banEnable',
                    data: {id:id,isRecormand:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

            // 全选操作
            $scope.all = function (m) {
                for(var i=0;i<$scope.pageInfos.length;i++){
                    if(m===true){
                        $scope.pageInfos[i].state=true;
                    }else {
                        $scope.pageInfos[i].state=false;
                    }
                }
            };
            // 获取复选框选中的值
            $scope.saveMakeUp = function (id) {

                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                $(".checkitem").each(function () {
                    if(this.checked){
                        console.log($(this).val());
                        batchUpdates.push($(this).val());
                    }
                })
                // 给用户打标签
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/addPostUserLabel',
                    data: {'id':id,'ids': batchUpdates.join(",")}
                }).then(function(result) {
                    console.log(result);
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            // $state.go('main.uc.banner.list');
                            $scope.firstApply1 = false;
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;

                });

            };

        } ]);